package com.zwr.sheller.customer.controller;

import com.zwr.sheller.core.common.JsonResult;
import com.zwr.sheller.core.common.LSYBusinessException;
import com.zwr.sheller.core.common.LSYExceptionCode;
import com.zwr.sheller.core.util.OSSUtil;
import com.zwr.sheller.customer.Service.RefundService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @Author LSY
 * @Date 2022/1/13 10:32
 * @Version 1.0
 **/
@RestController
@RequestMapping(value = "/refund")
@Api(tags = "前台用户退款接口集")
@CrossOrigin
public class RefundController {

    private static final String BUCKET = "http://47.108.60.93/ossImg/";

    RefundService refundService;
    @Autowired
    public RefundController(RefundService refundService) {
        this.refundService = refundService;
    }

    @PostMapping(value = "/refundPictureUpload")
    @ApiOperation(value = "上传退货申请图片接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "files", value = "图片数组", required = true, dataType = "List<MultipartFile>")
    })
    public Map<String, Object> refundPictureUpload(List<MultipartFile> files) throws LSYBusinessException, IOException {
        if(files.size() > 5) {
            throw new LSYBusinessException(LSYExceptionCode.TOO_MANY_PICTURE);
        }

        List<String> filesPath = new ArrayList<>();
        for (MultipartFile file : files) {
            String originalFilename = file.getOriginalFilename();

            if(originalFilename == null) {
                throw new LSYBusinessException(LSYExceptionCode.PICTURE_UPLOAD_FAILED);
            }
            String suffixName = originalFilename.substring(originalFilename.indexOf("."));
            String prefixName = UUID.randomUUID().toString().replace("-", "");
            String filePath = "temporary/" + prefixName + suffixName;
            OSSUtil.upload(filePath, file.getInputStream());

            filesPath.add(BUCKET + filePath);
        }

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("上传成功");
        jsonResult.setField("filesPath", filesPath);

        return jsonResult.getMap();
    }

    @PostMapping(value = "/applicationForReturn")
    @ApiOperation(value = "申请退货接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "oid", value = "需要退货的订单id", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "images", value = "退货图片的地址数组", required = true, dataType = "List<String>"),
            @ApiImplicitParam(name = "reason", value = "退货原因", required = true, dataType = "String"),
            @ApiImplicitParam(name = "detail", value = "详细设置", required = false, dataType = "String"),
            @ApiImplicitParam(name = "isReceive", value = "是否已收货", required = true, dataType = "String"),
    })
    public Map<String, Object> applicationForReturn(Long oid, @RequestParam List<String> images, String reason, String detail, Boolean isReceive) {
        refundService.applicationForReturn(oid, images, reason, detail, isReceive);

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("申请成功");

        return jsonResult.getMap();
    }
}
